package com.assisjrs.jpaandroid.persistence.converter;

import java.lang.reflect.Field;

import android.database.Cursor;

import com.assisjrs.jpaandroid.persistence.ConverterDatabase;
import com.assisjrs.jpaandroid.persistence.Entity;

public class CharacterConverter implements ConverterDatabase<Character, String> {
	@Override
	public String getFromInstance(final Field field, final Entity entity) {
		try {
			final Character value = (Character) field.get(entity);

			if (value == null)
				return null;

			return value.toString();
		} catch (final IllegalArgumentException e) {
			throw new RuntimeException(e);
		} catch (final IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Character getToInstance(final Cursor cursor, final int columnIndex, final Class<?> type) {
		final Short value = cursor.getShort(columnIndex);

		if (value == null)
			return null;

		return (char) value.shortValue();
	}
}
